<?php
namespace plugins\goods\controller;

use cmf\controller\PluginRestBaseController; //引用插件基类
use plugins\goods\model\GoodscatsModel;
use think\Db;

/**
 * api控制器
 */
class ApiGoodsCatsController extends PluginRestBaseController
{
    public $uploadpath = '';
    protected function _initialize()
    {
        // $this->uploadpath = ZY_APP_PATH.'plugins/goods/view/public/img/';
    }

    /**
     *获取商城分类
     */
    public function getGoodsCat($isModule = false)
    {
        $info = db('goods_cats')->where(['isshow' => 1])->field('catid,pid,catname,catimg')->order('`catsort`,`catid` asc')->select()->toArray();
        $r    = [];
        if (count($info) > 0) {
            foreach ($info as $k => $v) {
                $info[$k]['catimg'] = $info[$k]['catimg'] ? zy_domain() . $info[$k]['catimg'] : '';
            }
            $tree = new GoodscatsModel();
            $data = $tree->catetree($info);
            // require('classes/PHPTree.class.php');
            require dirname(__FILE__) . '/../classes/PHPTree.class.php';
            $r = \PHPTree::makeTree($data, array());
        }
        return zy_array(true, '操作成功！', $r, 200, $isModule);
    }

    /**
     * 商城分类
     */
    public function goodsCat($isModule = false)
    {
        $info = db('goods_cats')->where(['isshow' => 1])->field('catid,pid,catname,catimg')->order('`catsort`,`catid` asc')->select()->toArray();
        $r    = [];
        if (count($info) > 0) {
            foreach ($info as $k => $v) {
                $info[$k]['catimg'] = $info[$k]['catimg'] ? zy_domain() . $info[$k]['catimg'] : '';
            }
            $tree = new GoodscatsModel();
            $data = $tree->catetree($info);
        }
        return zy_array(true, '操作成功！', $data, 200, $isModule);
    }



    /**
     *获取推荐的主分类分类
     */
    public function recomGoodsCat($isModule = false)
    {
        $info = db('goods_cats')
            ->where(['isshow' => 1, 'isfloor' => 1])
            ->field('catid,pid,catname,catimg')
            ->order('`catsort`,`catid` asc')->select()->toArray();
        $r    = [];
        if (count($info) > 0) {
            foreach ($info as $k => $v) {
                $info[$k]['catimg'] = $info[$k]['catimg'] ? zy_domain() . $info[$k]['catimg'] : '';
            }
            $tree = new GoodscatsModel();

            $data = $tree->catetree($info);


            if (!empty($data)) {
                require dirname(__FILE__) . '/../classes/PHPTree.class.php';
                $r = \PHPTree::makeTree($data, array());
                $r = $this->referrGoods($r); //增加推荐商品列表
            }
        }
        return zy_array(true, '操作成功！', $r, 200, $isModule);
    }

    
    /**
     * 获取下级推荐分类id
     */
    public function getXiajiCats($data, $da = [])
    {
        if (!empty($data['children'])) {
            foreach ($data['children'] as $key => $value) {
                $da[] = $value['catid'];
                $da = $this->getXiajiCats($value, $da );
            }
        }
        return $da;
    }



    /**
     * 推荐商品
     */
    public function referrGoods( &$r )
    {
        //根据分类id获取下级推荐分类
        foreach ($r as $key => $value) {
            $xia = $this->getXiajiCats($value);
            $xia = array_merge($xia, [$value['catid']]);

            //获取商品列表
            $goods = new \plugins\goods\model\GoodsModel;
            $goods_list = $goods->where('catid', 'in', $xia)->where(
                [
                    'referr_status' => ['=', 1],
                    'audit_status' => ['=', 2],
                    'is_del' => ['<>', 1],
                    'status' => ['=', 1],
                ]
            )->order('add_time', 'desc')->select()->toArray();

            $value['goods_list'] = $goods_list;
            $r[$key] = $value;
        }

        return $r;
    }

    /**
     * 获取分类分类名称
     */
    public function catName()
    {
        $param = $this->request->param();
        if (empty($param['catid'])) {
            return api_json_echo(false, '缺少分类id', '', 100);
        }
        $catid = $param['catid'];
        $data  = db('goods_cats')->where('catid', $catid)->find()['catname'];
        if (empty($data)) {
            return api_json_echo(false, '分类不存在', '', 101);
        }
        return api_json_echo(true, '成功', $data, 200);
    }
}
